//
//  TicketBatchViewCell.m
//  quickBatchManager
//
//  Created by 王定波 on 15/10/15.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "TicketBatchViewCell.h"
#define RowHeight 40

@implementation TicketBatchViewCell
{
    UIView *mainView;
}

- (void)awakeFromNib {
    // Initialization code
    
    
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self initUI];
    }
    
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)initUI
{
    mainView=[[UIView alloc] init];
    mainView.translatesAutoresizingMaskIntoConstraints = NO;
    mainView.backgroundColor=[UIColor colorWithWhite:0.80 alpha:1];
    mainView.layer.borderWidth=1;
    mainView.layer.borderColor=[UIColor colorWithRed:0.49 green:0.40 blue:0.30 alpha:1].CGColor;
    [self.contentView addSubview:mainView];

    
    [self setEdge:mainView.superview firstView:mainView secondView:mainView.superview attr:NSLayoutAttributeTop constant:5];
    [self setEdge:mainView.superview firstView:mainView secondView:mainView.superview attr:NSLayoutAttributeBottom constant:-5];
    [self setEdge:mainView.superview firstView:mainView secondView:mainView.superview attr:NSLayoutAttributeLeft constant:0];
    [self setEdge:mainView.superview firstView:mainView secondView:mainView.superview attr:NSLayoutAttributeRight constant:0];
}


-(void)setData:(BatchProductModel*)data
{
    int i=100;
    
    for (UIView *view in mainView.subviews) {
            [view removeFromSuperview];
    }
    
    UILabel *batchTitle=[[UILabel alloc] init];
    
    batchTitle.backgroundColor=[UIColor colorWithRed:0.93 green:0.89 blue:0.84 alpha:1];
    batchTitle.textColor=[UIColor colorWithRed:0.49 green:0.40 blue:0.30 alpha:1];
    batchTitle.textAlignment=NSTextAlignmentLeft;
    batchTitle.font=[UIFont systemFontOfSize:17];
    batchTitle.translatesAutoresizingMaskIntoConstraints = NO;
    
    [mainView addSubview:batchTitle];
    
   
    batchTitle.text=[NSString stringWithFormat:@"   日期：%@  供货商：%@  数量：%.0f  金额：%@",[data.createDate substringToIndex:10],data.supplier?data.supplier.contactName:@"",data.totalNumber,[ToolUtils priceToStr:data.totalPrice]];
    
    
    
    [self setEdge:batchTitle.superview firstView:batchTitle secondView:batchTitle.superview attr:NSLayoutAttributeTop constant:0];
    [self setEdge:batchTitle.superview firstView:batchTitle secondView:batchTitle.superview attr:NSLayoutAttributeLeft constant:0];
    [self setEdge:batchTitle.superview firstView:batchTitle secondView:batchTitle.superview attr:NSLayoutAttributeRight constant:0];
    
    
    UIButton *editBtn=[[UIButton alloc] init];
    editBtn.translatesAutoresizingMaskIntoConstraints = NO;
    [editBtn setBackgroundImage:[UIImage imageNamed:@"btnyellow"] forState:UIControlStateNormal];
    [editBtn setTitle:@"修改" forState:UIControlStateNormal];
    [editBtn bk_whenTapped:^{
        PutInProductViewController *pipvc=[[PutInProductViewController alloc] init];
        pipvc.batch=data;
        [_parentVc.navigationController pushViewController:pipvc animated:YES];
    }];
    
    [mainView addSubview:editBtn];
    

    [self setEdge:editBtn.superview firstView:editBtn secondView:editBtn.superview attr:NSLayoutAttributeRight constant:-5];
    [self setEdge:mainView firstView:batchTitle secondView:editBtn attr:NSLayoutAttributeCenterY constant:0];
    [self setEdge:editBtn firstView:editBtn secondView:nil attr:NSLayoutAttributeHeight constant:30];
    [self setEdge:editBtn firstView:editBtn secondView:nil attr:NSLayoutAttributeWidth constant:60];
    
    
    
    
    NSArray *list=[AddProductModel searchWithWhere:@{@"batchId":[ToolUtils numberToStr:data.batchId]} orderBy:nil groupBy:@"productId" offset:0 count:0];
    
    UIView *preView=batchTitle;
    
   
    
    
    for (AddProductModel *object in list) {
        
        
        UIView *itemView=[[UIView alloc] init];
        itemView.translatesAutoresizingMaskIntoConstraints = NO;
        itemView.backgroundColor=[UIColor colorWithRed:1 green:0.97 blue:0.96 alpha:1];
        
        [mainView addSubview:itemView];
        
        UILabel *title1=[[UILabel alloc] init];
        title1.text=[object.createDate substringToIndex:16];
        title1.textColor=[UIColor colorWithRed:0.49 green:0.40 blue:0.30 alpha:1];
        title1.textAlignment=NSTextAlignmentCenter;
        title1.font=[UIFont systemFontOfSize:15];
        title1.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [itemView addSubview:title1];
        
        
        
        
        UILabel *title2=[[UILabel alloc] init];
        title2.text=object.product.modelNo;
        title2.textColor=title1.textColor;
        title2.textAlignment=title1.textAlignment;
        title2.font=title1.font;
        title2.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [itemView addSubview:title2];
        
        
        UILabel *title3=[[UILabel alloc] init];
        title3.text=[ToolUtils numberToStr:[AddProductModel sumWithWhere:@"productNumber" where:@{@"batchId":[ToolUtils numberToStr:object.batchId],@"productId":[ToolUtils numberToStr:object.productId]}]];
        title3.backgroundColor=title1.backgroundColor;
        title3.textColor=title1.textColor;
        title3.textAlignment=title1.textAlignment;
        title3.font=title1.font;
        title3.translatesAutoresizingMaskIntoConstraints = NO;
        
        [itemView addSubview:title3];
        
        
        UILabel *title4=[[UILabel alloc] init];
        title4.text=[ToolUtils priceToStr:object.buyPrice];
        title4.textColor=title1.textColor;
        title4.textAlignment=title1.textAlignment;
        title4.font=title1.font;
        title4.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [itemView addSubview:title4];
        
        
        
        UILabel *title5=[[UILabel alloc] init];
        title5.text=[ToolUtils priceToStr:[AddProductModel sumWithWhere:@"productNumber*buyPrice" where:@{@"batchId":[ToolUtils numberToStr:object.batchId],@"productId":[ToolUtils numberToStr:object.productId]}]];;
        title5.textColor=title1.textColor;
        title5.textAlignment=title1.textAlignment;
        title5.font=title1.font;
        title5.translatesAutoresizingMaskIntoConstraints = NO;
        
        
        [itemView addSubview:title5];
        
        
        UILabel *title6=[[UILabel alloc] init];
        title6.text=object.remark;
        title6.textColor=title1.textColor;
        title6.textAlignment=title1.textAlignment;
        title6.font=title1.font;
        title6.translatesAutoresizingMaskIntoConstraints = NO;
        
        [itemView addSubview:title6];
        
        
        //设置自动布局
        
        [self setEdge:title1.superview firstView:title1 secondView:title1.superview attr:NSLayoutAttributeTop constant:0];
        
        [self setEdge:title1.superview firstView:title1 secondView:title1.superview attr:NSLayoutAttributeLeft constant:0];
        
        [self setEdge:title6.superview firstView:title6 secondView:title6.superview attr:NSLayoutAttributeRight constant:0];
        
        
        
        //等宽高
        
        [self setEdge:itemView firstView:title2 secondView:title1 attr:NSLayoutAttributeWidth constant:0];
        [self setEdge:itemView firstView:title3 secondView:title1 attr:NSLayoutAttributeWidth constant:0];
        [self setEdge:itemView firstView:title4 secondView:title1 attr:NSLayoutAttributeWidth constant:0];
        [self setEdge:itemView firstView:title5 secondView:title1 attr:NSLayoutAttributeWidth constant:0];
        [self setEdge:itemView firstView:title6 secondView:title1 attr:NSLayoutAttributeWidth constant:0];
        
        
        
        [itemView addConstraint:[NSLayoutConstraint constraintWithItem:title1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title2 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [itemView addConstraint:[NSLayoutConstraint constraintWithItem:title2 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title3 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [itemView addConstraint:[NSLayoutConstraint constraintWithItem:title3 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title4 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [itemView addConstraint:[NSLayoutConstraint constraintWithItem:title4 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title5 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [itemView addConstraint:[NSLayoutConstraint constraintWithItem:title5 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:title6 attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        
        
        [self setEdge:itemView firstView:title1 secondView:itemView attr:NSLayoutAttributeCenterY constant:0];
        [self setEdge:itemView firstView:title2 secondView:title1 attr:NSLayoutAttributeCenterY constant:0];
        [self setEdge:itemView firstView:title3 secondView:title1 attr:NSLayoutAttributeCenterY constant:0];
        [self setEdge:itemView firstView:title4 secondView:title1 attr:NSLayoutAttributeCenterY constant:0];
        [self setEdge:itemView firstView:title5 secondView:title1 attr:NSLayoutAttributeCenterY constant:0];
        [self setEdge:itemView firstView:title6 secondView:title1 attr:NSLayoutAttributeCenterY constant:0];
        
        
        
        
        [self setEdge:itemView.superview firstView:itemView secondView:preView attr:NSLayoutAttributeHeight constant:0];
        [mainView addConstraint:[NSLayoutConstraint constraintWithItem:preView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTop multiplier:1.0 constant:-1]];
        
        [self setEdge:itemView.superview firstView:itemView secondView:itemView.superview attr:NSLayoutAttributeLeft constant:0];
        [self setEdge:itemView.superview firstView:itemView secondView:itemView.superview attr:NSLayoutAttributeRight constant:0];
        
        
        preView=itemView;
        
        itemView.tag=i;
        i++;
        
    }
    
    [self setEdge:preView.superview firstView:preView secondView:preView.superview attr:NSLayoutAttributeBottom constant:0];
    
}
//自动布局快速添加
- (void)setEdge:(UIView*)supperView firstView:(UIView*)firstView secondView:(UIView*)secondView attr:(NSLayoutAttribute)attr constant:(CGFloat)constant
{
    if(secondView==nil)
        [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:constant]];
    else [supperView addConstraint:[NSLayoutConstraint constraintWithItem:firstView attribute:attr relatedBy:NSLayoutRelationEqual toItem:secondView attribute:attr multiplier:1.0 constant:constant]];
}

@end
